﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace Zuoye
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.Cookies["account"] != null & Request.Cookies["pwd"] != null)
            {
                string account = Request.Cookies["account"].Value;
                string pwd = Request.Cookies["pwd"].Value;
                DataSet ds = DbHelper.Query($@"select u.*,c.CityName as city_name  from users u inner join S_City c on c.CityID=u.city_id
                                           where u.account = '{account}' and u.pwd = '{pwd}'");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    //4.存session
                    Session["User"] = ds.Tables[0].Rows[0];//把当前登录人的整行字段信息全部存到session
                    //5.跳页
                    Response.Redirect("Index.aspx");
                }
                }
        }

        protected void btnlogin_Click(object sender, EventArgs e)
        {
            string account = txtlgname.Text.Trim();
            string pwd = txtlgpwd.Text.Trim();
            string code = Session["Code"].ToString();
            string userCode=txtCode.Text.Trim();
            if (userCode != code)
            {
                Response.Write("<script>alert('验证码错误')</script>");
                return;
            }
            DataSet ds = DbHelper.Query($@"select u.*,c.CityName as city_name  from users u inner join S_City c on c.CityID=u.city_id
                                           where u.account = '{account}' and u.pwd = '{pwd}'");
            if (ds.Tables[0].Rows.Count == 0)
            {
                Response.Write("<script>alert('账号密码错误')</script>");
                return;
            }
            int type = Convert.ToInt32(ds.Tables[0].Rows[0]["type"]);
            if (type == 0)
            {
                Response.Write("<script>alert('请等待管理人员审核')</script>");
                return;
            }
                Session["User"] = ds.Tables[0].Rows[0];
                if (CheckRem.Checked)
                {
                //账号密码存到cookie里
                HttpCookie accoutCookie = new HttpCookie("account");
                accoutCookie.Value = account;
                accoutCookie.Expires = DateTime.Now.AddMonths(1);
                HttpCookie pwdCookie = new HttpCookie("pwd");
                pwdCookie.Value = account;
                pwdCookie.Expires = DateTime.Now.AddMonths(1);
                Response.Cookies.Add(accoutCookie);
                Response.Cookies.Add(pwdCookie);

            }
                Response.Redirect("Index.aspx");
            }

        }
    }
